<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );


require_once('./administrator/components/com_realestatemanager/admin.realestatemanager.class.others.php');
require_once ( JPATH_BASE .DS.'administrator'.DS.'components'.DS.'com_realestatemanager'.DS.'admin.realestatemanager.class.others.php' );
require_once ( JPATH_BASE .DS.'administrator'.DS.'components'.DS.'com_realestatemanager'.DS.'functions.php' );
require_once ( JPATH_BASE .DS.'components'.DS.'com_realestatemanager'.DS.'realestatemanager.main.categories.class.php' );
$doc =& JFactory::getDocument(); 
$doc->addStyleSheet( JURI::base(true) .'/'. 'administrator'.'/'. 'components'.'/'.'com_realestatemanager'.'/'.'includes'.'/'.'realestatemanager.css' );

$option = "com_realestatemanager" ;

if ( !isset($mosConfig_absolute_path) )  $mosConfig_absolute_path = JPATH_SITE;
// load language
$lang_def_en=0;
$lang =& JFactory::getLanguage();
foreach ($lang->getLocale() as $locale) {
          $mosConfig_lang=$locale;
    if (file_exists($mosConfig_absolute_path."/components/com_realestatemanager/language/{$mosConfig_lang}.php" ))     {  
          include_once($mosConfig_absolute_path."/components/com_realestatemanager/language/{$mosConfig_lang}.php" );
          $lang_def_en=1;
           break;
    } 
}
if ( $lang_def_en != 1 ) {
   $mosConfig_lang = "english";  
   include_once($mosConfig_absolute_path."/components/com_realestatemanager/language/english.php" );
 }
 
//  $doc->addScript(JURI::base(true).'/includes/js/calendar/calendar.js');
//  $doc->addScript(JURI::base(true).'/includes/js/calendar/lang/calendar-en-GB.js');
//  $doc->addStyleSheet(JURI::base(true).'/includes/js/calendar/calendar-mos.css');


if( !function_exists( 'sefRelToAbs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}

if( !function_exists( 'real_categoryArray')) {
  function real_categoryArray()
  {
    global $database;
    // get a list of the menu items
    $query = "SELECT c.*, c.parent_id AS parent"
    . "\n FROM #__rem_main_categories c"
    . "\n WHERE section='com_realestatemanager'"
    . "\n AND published <> -2"
    . "\n ORDER BY ordering" ;
    $database->setQuery($query);
    $items = $database->loadObjectList();
    // establish the hierarchy of the menu
    $children = array();
    // first pass - collect children
    foreach ($items as $v) {
      $pt = $v->parent;
      $list = @$children[$pt] ? $children[$pt] : array();
      array_push($list, $v);
      $children[$pt] = $list;
    }
    // second pass - get an indent list of the items
    $array = mosTreeRecurse(0, '', array(), $children);
  
    return $array;
  }
}

if( !function_exists( 'mod_real_categoryTreeList')) {
  function mod_real_categoryTreeList($id, $action,$is_new, $options = array())
  {
    global  $database;
    $list = real_categoryArray();
    $cat = new mainRealEstateCategories($database);
    $cat->load($id);
  
    $this_treename = '';
    $childs_ids =  Array();
    foreach ($list as $item) {
      if ($item->id == $cat->id || array_key_exists($item->parent_id,$childs_ids) )
      $childs_ids[$item->id] = $item->id;
    }
  
    foreach ($list as $item) {
      if ($this_treename) {
        if ($item->id != $cat->id
          && strpos($item->treename, $this_treename) === false
          && array_key_exists($item->id,$childs_ids) === false) {
          $options[] = mosHTML::makeOption($item->id, $item->treename);
        }
      } else {
        if ($item->id != $cat->id) {
          $options[] = mosHTML::makeOption($item->id, $item->treename);
        } else {
          $this_treename = "$item->treename/";
        }
      }
    }
  
    $parent= null;
    $parent = mosHTML::selectList($options, 'catid',
  'class="inputbox" size="1"', 'value', 'text', $cat->parent_id);
    return $parent;
  }
}

	global $mosConfig_absolute_path, $mosConfig_allowUserRegistration,$mosConfig_lang, $database;

	include('./administrator/components/com_realestatemanager/compat.joomla1.5.php');


$showCategories	= $params->get ('showCategories', 0);
$showListingStatus	= $params->get ('showListingStatus', 0);
$showPropertyOwnership	= $params->get ('showPropertyOwnership', 0);
$showProviderClass= $params->get ('showProviderClass', 0);
$showZoning	= $params->get ('showZoning', 0);
$showStyle	= $params->get ('showStyle', 0);
$showAvailableForRent	= $params->get ('showAvailableForRent', 0);
$showYear= $params->get ('showYear', 0);
$showPrice	= $params->get ('showPrice', 0);
$showTitle  = $params->get ('showTitle', 0);
$showAddress  = $params->get ('showAddress', 0);
$showDescription  = $params->get ('showDescription', 0);
$showBroker  = $params->get ('showBroker', 0);
$showFeature  = $params->get ('showFeature', 0);
$showAgent  = $params->get ('showAgent', 0);
$showArea  = $params->get ('showArea', 0);
$showHouseId  = $params->get ('showHouseId', 0);
$showOwner  = $params->get ('showOwner', 0);
$showModel  = $params->get ('showModel', 0);
$showCountry  = $params->get ('showCountry', 0);
$showRegion  = $params->get ('showRegion', 0);
$showCity   = $params->get ('showCity', 0); 
$showAdvanceSearch = $params->get ('showAdvanceSearch', 0);
$ItemId_tmp_from_params=$params->get ('ItemId');
$moduleclass_sfx=$params-> get('moduleclass_sfx');




	$categories[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $database->setQuery("SELECT id  FROM #__menu WHERE   link LIKE'%option=com_realestatemanager%' AND params LIKE '%back_button%'  ");
  $ItemId_tmp_from_db = $database->loadResult();  
    if($ItemId_tmp_from_params==""){
      $ItemId=$ItemId_tmp_from_db;
    }
    else{
      $ItemId=$ItemId_tmp_from_params;
    } 
  
  
  
	$clist = mod_real_categoryTreeList(0,'',true,$categories);
	




  global $mainframe, $database, $my;
  global $mosConfig_shownoauth, $mosConfig_live_site, $mosConfig_absolute_path;
  global $cur_template;
  $currentcat = NULL;
  // Parameters
  $menu = new mosMenu( $database );
  $menu->load( $ItemId );
  $params = new mosParameters( $menu->params );
//  $params->def( 'header', $menu->name );
  $params->def( 'pageclass_sfx', '' );
  //
  $params->def( 'show_search', '1' );
 // $params->def( 'back_button', $mainframe->getCfg( 'back_button' ) );

  $pathway =  sefRelToAbs('index.php?option=' . $option .
      '&amp;task=show_search&amp;Itemid=' . $ItemId);
  $pathway_name =  _REALESTATE_MANAGER_LABEL_SEARCH ;
// $mainframe->appendPathWay($pathway_name,$pathway);
  
  $currentcat->descrip = " ";
  $currentcat->align = 'right';
  
  // page image
  $currentcat->img = "./components/com_realestatemanager/images/rem_logo.png";


  $currentcat->header = $params->get( 'header' );
  $currentcat->header = $currentcat->header . ": " . _REALESTATE_MANAGER_LABEL_SEARCH;
  
  // used to show table rows in alternating colours
  $tabclass = array( 'sectiontableentry1', 'sectiontableentry2' );
  
  $year[] = mosHTML :: makeOption(' ', _REALESTATE_MANAGER_SHOW_SEARCH_FROM);
  $year_to[] = mosHTML :: makeOption(' ', _REALESTATE_MANAGER_SHOW_SEARCH_TO);
  for($i=date('Y');$i>=1900;$i--)
  {
    $year[] = mosHTML :: makeOption($i, $i);
    $year_to[] = mosHTML :: makeOption($i, $i);
  }
  $yearlist = mosHTML :: selectList($year, 'yearfrom', 'class="inputbox" size="1"', 'value', 'text');
  $yearlistto = mosHTML :: selectList($year_to, 'yearto', 'class="inputbox" size="1"', 'value', 'text');
  // show the House
  $params->def('yearlist',$yearlist);
  $params->def('yearlistto',$yearlistto);
  
	// <-- DISTRITOS
	$query = "SELECT id, designacao FROM   #__districts ";
	$database->setQuery($query);
	$temp = $database->loadObjectList();
	$districts = array();
	$districts[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
	foreach($temp as $value){
		//$districts[$value->id] = $value->designacao;
		$districts[] = mosHtml::makeOption($value->id, $value->designacao);
	}
	$districts = mosHTML :: selectList($districts, 'district', 'class="inputbox" size="1"', 'value', 'text', '');
	$params->def('district',$districts);  
  	// -->
  
    //price 
  $db =& JFactory::getDBO();
  $query = "SELECT price  FROM   #__rem_houses ";
  $database->setQuery($query);
  $prices = $database->loadResultArray();
  
  rsort($prices,SORT_NUMERIC);
  $max_price = $prices[0];


  $price[] = mosHTML :: makeOption(' ', _REALESTATE_MANAGER_SHOW_SEARCH_FROM);
  $price_to[] = mosHTML :: makeOption(' ', _REALESTATE_MANAGER_SHOW_SEARCH_TO);

  for( $i=0; $i<=$max_price+10000; $i=$i+10000){
  $price[] = mosHTML :: makeOption($i, number_format($i, 0, ",", ".") . " €");
  $price_to[] = mosHTML :: makeOption($i, number_format($i, 0, ",", ".") ." €");
  }

  $pricelist = mosHTML :: selectList($price, 'pricefrom', 'class="inputbox" size="1"', 'value', 'text');
  $params->def('pricefrom',$pricelist);
  $pricelistto = mosHTML :: selectList($price_to, 'priceto', 'class="inputbox" size="1"', 'value', 'text');
  $params->def('priceto',$pricelistto);
  
  
  
  //Select list for house type
  $listing_type[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $listing_type[] = mosHtml::makeOption("foreclosure",_REALESTATE_MANAGER_OPTION_FORECLOSURE);
  $listing_type[] = mosHtml::makeOption("house for rent",_REALESTATE_MANAGER_OPTION_HOUSE_FOR_RENT);
  $listing_type[] = mosHtml::makeOption('room for rent',_REALESTATE_MANAGER_OPTION_ROOM_FOR_RENT);
  $listing_type[] = mosHtml::makeOption('for sale',_REALESTATE_MANAGER_OPTION_FOR_SALE);
  $listing_type[] = mosHtml::makeOption('new home',_REALESTATE_MANAGER_OPTION_NEW_HOME);
  $listing_type[] = mosHtml::makeOption("sublet",_REALESTATE_MANAGER_OPTION_SUBLET);
  $house_type_list = mosHTML :: selectList($listing_type, 'listing_type', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('house_type_list',$house_type_list);
  
  //Select list for house price type
  $price_type[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $price_type[] = mosHtml::makeOption("negotiable",_REALESTATE_MANAGER_OPTION_NEGOTIABLE);
  $price_type[] = mosHtml::makeOption("starting",_REALESTATE_MANAGER_OPTION_STARTING);
  $house_price_type = mosHTML :: selectList($price_type, 'price_type', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('house_price_type',$house_price_type);

  
  //Select list for house listing status type
  $listing_status_type[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $listing_status_type[] = mosHtml::makeOption("active",_REALESTATE_MANAGER_OPTION_ACTIVE);
  $listing_status_type[] = mosHtml::makeOption("offer",_REALESTATE_MANAGER_OPTION_OFFER);
  $listing_status_type[] = mosHtml::makeOption("contract",_REALESTATE_MANAGER_OPTION_CONTRACT);
  $listing_status_type[] = mosHtml::makeOption("closed",_REALESTATE_MANAGER_OPTION_CLOSED);
  $listing_status_type[] = mosHtml::makeOption("withdrawn",_REALESTATE_MANAGER_OPTION_WITHDRAWN);
  $listing_status_type = mosHTML :: selectList($listing_status_type, 'listing_status', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('listing_status_type',$listing_status_type);  
  
  //Select list for house listing property type
  $property_type[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $property_type[] = mosHtml::makeOption("townhouse",_REALESTATE_MANAGER_OPTION_TOWNHOUSE);
  $property_type[] = mosHtml::makeOption("apartment",_REALESTATE_MANAGER_OPTION_APARTMENT);
  $property_type[] = mosHtml::makeOption("warehouse",_REALESTATE_MANAGER_OPTION_WAREHOUSE);
  $property_type[] = mosHtml::makeOption("office",_REALESTATE_MANAGER_OPTION_OFFICE);
  $property_type[] = mosHtml::makeOption("garage",_REALESTATE_MANAGER_OPTION_GARAGE);
  $property_type[] = mosHtml::makeOption("store",_REALESTATE_MANAGER_OPTION_STORE);
  $property_type[] = mosHtml::makeOption("houseband", _REALESTATE_MANAGER_OPTION_HAOUSE_BAND);
  $property_type[] = mosHtml::makeOption("housegav", _REALESTATE_MANAGER_OPTION_HOUSE_GAV);
  $property_type[] = mosHtml::makeOption("housegem",_REALESTATE_MANAGER_OPTION_HOUSE_GEM);
  $property_type[] = mosHtml::makeOption("houseind",_REALESTATE_MANAGER_OPTION_IND_HOUSE);
  $property_type[] = mosHtml::makeOption("parking",_REALESTATE_MANAGER_OPTION_PARKING);
  $property_type[] = mosHtml::makeOption("pavillion",_REALESTATE_MANAGER_OPTION_PAVILLION);
  $property_type[] = mosHtml::makeOption("building",_REALESTATE_MANAGER_OPTION_BUILDING);
  $property_type[] = mosHtml::makeOption("farm",_REALESTATE_MANAGER_OPTION_FARM);
  $property_type[] = mosHtml::makeOption("smallfarm",_REALESTATE_MANAGER_OPTION_SMALLFARM);
  $property_type[] = mosHtml::makeOption("rest",_REALESTATE_MANAGER_OPTION_REST);
  $property_type[] = mosHtml::makeOption("land",_REALESTATE_MANAGER_OPTION_LAND);
  $property_type = mosHTML :: selectList($property_type, 'property_ownership', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('property_type',$property_type);  
	
	$typology[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL_F);
	$typology[] = mosHtml::makeOption("t0", "T0");
	$typology[] = mosHtml::makeOption("t0+1", "T0+1");
	$typology[] = mosHtml::makeOption("t1", "T1");
	$typology[] = mosHtml::makeOption("t1+1", "T1+1");
	$typology[] = mosHtml::makeOption("t2", "T2");
	$typology[] = mosHtml::makeOption("t2+1", "T2+1");
	$typology[] = mosHtml::makeOption("t3", "T3");
	$typology[] = mosHtml::makeOption("t3+1", "T3+1");
	$typology[] = mosHtml::makeOption("t4", "T4");
	$typology[] = mosHtml::makeOption("t4+1", "T4+1");
	$typology[] = mosHtml::makeOption(">=t5", ">=T5");
	$typology[] = mosHtml::makeOption("sem", "Outros");
	$typology = mosHTML :: selectList($typology, 'typology', 'class="inputbox" size="1"', 'value', 'text', '');
	$params->def('typology',$typology);  

  //Select list for house listing status type
  $provider_class[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $provider_class[] = mosHtml::makeOption("agent",_REALESTATE_MANAGER_OPTION_AGENT);
  $provider_class[] = mosHtml::makeOption("aggregator",_REALESTATE_MANAGER_OPTION_AGGREGATOR);
  $provider_class[] = mosHtml::makeOption("broker",_REALESTATE_MANAGER_OPTION_BROKER);
  $provider_class[] = mosHtml::makeOption("franchisor",_REALESTATE_MANAGER_OPTION_FRANCHISOR);
  $provider_class[] = mosHtml::makeOption("homebuilder",_REALESTATE_MANAGER_OPTION_HOMEBUILDER);
  $provider_class[] = mosHtml::makeOption("publisher",_REALESTATE_MANAGER_OPTION_PUBLISHER);
  $provider_class = mosHTML :: selectList($provider_class, 'provider_class', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('provider_class',$provider_class);  


  //Select list for house area
  $zoning[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $zoning[] = mosHtml::makeOption("agricultural",_REALESTATE_MANAGER_OPTION_AGRICULTURAL);
  $zoning[] = mosHtml::makeOption("commercial",_REALESTATE_MANAGER_OPTION_COMMERCIAL);
  $zoning[] = mosHtml::makeOption("industrial",_REALESTATE_MANAGER_OPTION_INDUSTRIAL);
  $zoning[] = mosHtml::makeOption("recreational",_REALESTATE_MANAGER_OPTION_RECREATIONAL);
  $zoning[] = mosHtml::makeOption("residential",_REALESTATE_MANAGER_OPTION_RESIDENTIAL);
  $zoning[] = mosHtml::makeOption("unincorporated",_REALESTATE_MANAGER_OPTION_UNINCORPORATED);
  $zoning = mosHTML :: selectList($zoning, 'zoning', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('zoning',$zoning);  

  
  //Select list for house style
  $style[] = mosHtml::makeOption('all',_REALESTATE_MANAGER_LABEL_ALL);
  $style[] = mosHtml::makeOption("Cape Cod",_REALESTATE_MANAGER_OPTION_CAPE_COD);
  $style[] = mosHtml::makeOption("Colonial",_REALESTATE_MANAGER_OPTION_COLONIAL);
  $style[] = mosHtml::makeOption("Craftsman",_REALESTATE_MANAGER_OPTION_CRAFTSMAN);
  $style[] = mosHtml::makeOption("Gothic",_REALESTATE_MANAGER_OPTION_GOTHIC);
  $style[] = mosHtml::makeOption("Prairie",_REALESTATE_MANAGER_OPTION_PRAIRIE);
  $style[] = mosHtml::makeOption("Ranch",_REALESTATE_MANAGER_OPTION_RANCH2);
  $style[] = mosHtml::makeOption("Split Level",_REALESTATE_MANAGER_OPTION_SPLIT_LEVEL);
  $style[] = mosHtml::makeOption("Tudor",_REALESTATE_MANAGER_OPTION_TUDOR);
  $style[] = mosHtml::makeOption("Victorian Queen Anne",_REALESTATE_MANAGER_OPTION_VICTORIAN_QUEEN_ANNE);
  $style = mosHTML :: selectList($style, 'style', 'class="inputbox" size="1"', 'value', 'text', '');
  $params->def('style',$style); 
?>
<div class="com_realestatemanager<?php echo $moduleclass_sfx;?>">
	<form method="get" id="form_1" name="mod_realestatlibsearchForm" action="<?php echo sefRelToAbs("index.php?option=com_realestatemanager&amp;task=search&amp;catid=0&amp;Itemid=".$ItemId); ?>">
	<input type="hidden" name="option" value="com_realestatemanager">
	<input type="hidden" name="task" value="search">
	<input type="hidden" name="Itemid" value="<?php echo $ItemId ?>">
	<div class="pad1">
		<h3>Pesquisa</h3>

		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_CATEGORY;?>:<br />
			<?php echo $clist;?>
		</div>

		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_EDITION;?>:<br />
			<?php  echo $params->get('property_type');?>
		</div>

		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_TYPOLOGY;?>:<br />
			<?php echo $params->get('typology');?>
		</div>

		<div class="row_select">
			<div class="cols">
				<?php echo _REALESTATE_MANAGER_LABEL_PRICE;?>:<br>
				<?php echo $params->get('pricefrom')?>
			</div>
			<div class="cols pad_left1">
				&nbsp;<br>
				<?php echo $params->get('priceto')?>
			</div>
		</div>
		
		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_DISTRICT;?>:<br />
			<?php echo $params->get('district');?>
		</div>
		
		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_COUNCIL;?>:<br />
			<select name="council" id="council">
			<option value="all"><?php echo _REALESTATE_MANAGER_LABEL_ALL;?></option>
			</select>
		</div>
		
		<div class="row_select">
			<?php echo _REALESTATE_MANAGER_LABEL_PARISH;?>:<br />
			<select name="parish" id="parish">
			<option value="all"><?php echo _REALESTATE_MANAGER_LABEL_ALL_F;?></option>
			</select>
		</div>
		
		<div class="cols">
			Referência:<br>
			<input type="text" name="houseid" id="houseid" style="width:100px" />
		</div>
		<div class="cols pad_left1">
			<input type="submit" value="<?php echo _REALESTATE_MANAGER_BUTTON_SEARCH; ?>" class="button" />
		</div>
		
	</div>
</form>
<script type="text/javascript">
$('#district').change(function(){
	$.get('index.php/component/realestatemanager/?option=com_realestatemanager&task=get_councils&tmpl=blank&id_district='+$('#district').val(), function(data){
		$('#council').html(data);
		$('#parish').html("<option value=\"all\"><?php echo _REALESTATE_MANAGER_LABEL_ALL_F;?></option>");
	});
});
$('#council').change(function(){
	$.get('index.php/component/realestatemanager/?option=com_realestatemanager&task=get_parish&tmpl=blank&id_council='+$('#council').val(), function(data){
		$('#parish').html(data);
	});
});

</script>
<?php /*?><br>
<div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by Ordasoft! </a></div>
</div><?php */?>
